package org.jeecg.modules.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.customer.entity.BusCustomer;
import org.jeecg.modules.customer.mapper.BusCustomerMapper;
import org.jeecg.modules.customer.service.IBusCustomerService;
import org.jeecg.modules.demo.points.entity.BusVipPointDetail;
import org.jeecg.modules.demo.points.service.IBusVipPointDetailService;
import org.jeecg.modules.demo.totalpoints.entity.BusVipPoint;
import org.jeecg.modules.demo.totalpoints.service.IBusVipPointService;
import org.jeecg.modules.message.wechat.FuWuHaoMessageService;
import org.jeecg.modules.order.entity.BusOrder;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description: 客户表
 * @Author: jeecg-boot
 * @Date: 2021-07-15
 * @Version: V1.0
 */
@Service
public class BusCustomerServiceImpl extends ServiceImpl<BusCustomerMapper, BusCustomer> implements IBusCustomerService {

    @Autowired
    ISysUserService sysUserService;

    @Autowired
    IBusVipPointService vipPointService;

    @Autowired
    IBusVipPointDetailService detailService;

    @Autowired
    FuWuHaoMessageService fuWuHaoMessageService;

    @Override
    @Transactional
    public void saveBusAndSysUser(BusCustomer busCustomer) {
        SysUser sysUser = new SysUser();
        sysUser.setSex(busCustomer.getGender());
        sysUser.setEmail(busCustomer.getCusEmail());
        sysUser.setPhone(busCustomer.getCusTel());
        sysUser.setAvatar(busCustomer.getAvatarUrl());
        String salt = oConvertUtils.randomGen(8);
        sysUser.setSalt(salt);
        String passwordEncode = PasswordUtil.encrypt(busCustomer.getCusTel(), "123456", salt);
        sysUser.setPassword(passwordEncode);
        sysUser.setStatus(1);
        sysUser.setDelFlag(CommonConstant.DEL_FLAG_0);
        sysUser.setUsername(busCustomer.getCusTel());
        sysUser.setRealname(busCustomer.getCusName());
        sysUser.setPost("biosune_cus");
//        sysUser.setOrgCode("A02A02");
//        sysUser.setOrgCodeTxt()
        sysUserService.saveUser(sysUser, "1412241899883839489", "1375483a30a147d58fceab6263c88971");
        busCustomer.setSysUserId(sysUser.getId());
        busCustomer.setManagerType(0);
        save(busCustomer);
        //注册送积分
        updatePointsByOrder(busCustomer,sysUser);
    }


    private void updatePointsByOrder(BusCustomer customer, SysUser sysUser) {
//        BigDecimal price = order.getAmount();
//        Integer points = orderPointsService.calPoints(price.intValue(), order.getSampleNum(), direction);
//        String cusId = order.getCusId();
        Integer points = 300;

        LambdaQueryWrapper<BusVipPoint> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BusVipPoint::getUserId, customer.getSysUserId());
        BusVipPoint busVipPoint = vipPointService.getOne(wrapper);
        if (busVipPoint == null) {
            busVipPoint = new BusVipPoint();
            busVipPoint.setUserId(customer.getSysUserId());
            busVipPoint.setPoints(points);
            busVipPoint.setUseablePoints(0);
            busVipPoint.setFreeRegPoint(0);
            busVipPoint.setCreateBy(sysUser.getUsername());
            busVipPoint.setSysOrgCode(sysUser.getOrgCode());
            vipPointService.save(busVipPoint);

        } else {
            //说明已经注册过，不在赠送
        }

        BusVipPointDetail detail = new BusVipPointDetail();

        detail.setInOut(1);
        detail.setUserId(customer.getSysUserId());
        detail.setAmount(points);
        detail.setCreateTime(new Date());
        detail.setUserPhone(sysUser.getPhone());
        detailService.save(detail);

        //推送积分变动信息到客户端
        fuWuHaoMessageService.sendPointsMessage("wx7ac6ed39d0d2af6d", customer.getOpenId(), detail, busVipPoint);
    }
}
